import * as VueRouter from 'vue-router'

// 查看localStorage中是否有token，没有就去登录
function isAuthenticated () {
  return localStorage.getItem('token')
}

const routes = [
  {
    path: '/',
    name: 'Home',
    component: () => import('../page/Home.vue'),
    beforeEnter: (to, from, next) => {
      if (to.name !== 'Login' && !isAuthenticated()) next({ name: 'Login' })
      else next()
    },
    redirect: { name: 'MyTeam' },
    children: [
      {
        path: '/myteam',
        name: 'MyTeam',
        component: () => import('../components/home/main/MyTeam.vue')
      },{
        path: '/teammembers',
        name: 'TeamMembers',
        component: () => import('../components/home/main/TeamMembers.vue')
      }, {
        path: '/followteam',
        name: 'FollowTeam',
        component: () => import('../components/home/main/FollowTeam.vue')
      }, {
        path: '/my',
        name: 'My',
        component: () => import('../components/home/main/My.vue')
      }
    ]
  }, {
    path: '/login',
    name: 'Login',
    component: () => import('../page/Login.vue')
  }, {
    path: '/register',
    name: 'Register',
    component: () => import('../page/Register.vue')
  }
]

export default VueRouter.createRouter({
  history: VueRouter.createWebHashHistory(),
  routes
})
